<?php

namespace Admin\Model;
use Think\Model;

class ArticleModel extends Model
{
    protected $tableName = "article";
    protected $tablePrefix = "dangjian_";
    protected $showRow = 10;
    //查询所有文章
    public function queryAllArt ($data)
    {
        $name = $data['name'];//作者
        $sort = $data['sort'];//文章分类
        $time = $data['time'];//发布时间范围
        $is_show = $data['is_show'];//隐藏或显示
        $order = $data['order'];//排序字段
        $order1 = $data['order1'];//排序方式
        $map = array();
        if(!empty($name)){
            $map['u.name'] = array("LIKE", '%' . $name . '%');
        }
        if(!empty($sort)){
            $map['a.sort_id'] = $sort;
        }
        if(!empty($time)){
            $start = substr($time,0,10);
            $end = substr($time,13,10);
            $start = date("Y-m-d H:i:s",strtotime($start));
            $end = date("Y-m-d H:i:s",strtotime($end));
            $map['a.publish_time'] = array(array("gt",$start),array("lt",$end),"and");
        }
        if(!empty($is_show)){
            if($is_show == 2){
                $is_show = 0;
            }
            $map['a.is_show'] = $is_show;
        }

        if(empty($order)){
            $order = "a.id";
        }

        if(empty($order1)){
            $order1 = "asc";
        }

        $count = $this->alias("a")
            ->join("dangjian_article_sort ass on ass.id = a.sort_id")
            ->join("dangjian_admin_user u on a.user_id = u.id")
            ->where($map)
            ->count();
        $page = new \Think\Page($count,$this->showRow);
        $show = $page -> show();

        $list = $this->alias("a")
            ->join("dangjian_article_sort ass on ass.id = a.sort_id")
            ->join("dangjian_admin_user u on a.user_id = u.id")
            ->field("a.id, a.title, a.is_show, a.publish_time, ass.name sort_name, u.name")
            ->order("$order $order1")
            ->where($map)
            ->limit($page->firstRow.','.$page->listRows)
            ->select();
//        echo $list;die;
        $res['show'] = $show;
        $res['list'] = $list;
        return $res;
    }

    //更改文章是否显示
    public function updateShow($id,$is_show)
    {
        $this->where("id=$id")->save(array('is_show'=>$is_show));
    }

    public function queryArtInfoById ($id)
    {
        $info = $this->alias("a")
            ->join("dangjian_article_sort ass on ass.id = a.sort_id")
            ->join("dangjian_admin_user u on a.user_id = u.id")
            ->field("a.title, a.content, ass.name sort_name, u.name")
            ->where("a.id=$id")
            ->order("a.publish_time desc")
            ->find();
//        echo "<pre>";
//        print_r($info);die;
        return $info;
    }

    //小程序前台首页显示2篇文章,按发布时间倒序
    public function queryAllArticle ()
    {
        $res = $this
            ->field("id,title,description,thumbnail")
            ->order("publish_time desc")
            ->where("is_show=1")
            ->limit(2)
            ->select();
        return $res;
    }
    //小程序前台显示所有文章,按发布时间倒序
    public function queryAllArticles ()
    {
        $res = $this
            ->field("id,title,description,thumbnail")
            ->order("publish_time desc")
            ->where("is_show=1")
            ->select();
        return $res;
    }

    public function saveArt ($data)
    {
        return $res = $this->add($data);
    }
}